package com.imooc.coupon.executor.impl;

import com.imooc.coupon.support.vo.CouponTemplateSDK;
import com.imooc.coupon.support.vo.SettlementInfo;
import com.imooc.coupon.constant.RuleFlag;
import com.imooc.coupon.executor.AbstractRuleExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 折扣优惠券结算规则执行器
 *
 * Created By TINY
 * Create Time 2020/2/12
 **/
@Slf4j
@Component
public class DiscountExecutor extends AbstractRuleExecutor {
    /**
     * 优惠券结算规则类型标记
     *
     * @return {@link RuleFlag} 优惠券结算规则标记
     */
    @Override
    public RuleFlag supports() {
        return RuleFlag.DISCOUNT;
    }

    /**
     * 核心的折扣优惠券规则计算逻辑实现
     *
     * @param settlementInfo {@link SettlementInfo} 结算信息对象
     * @return {@link SettlementInfo} 进行计算处理后的结算信息对象
     */
    @Override
    protected SettlementInfo cProcess(SettlementInfo settlementInfo) {
        // 折扣优惠券可以直接使用（无门槛）
        CouponTemplateSDK templateSDK = settlementInfo
                .getCouponAndTemplateInfos().get(0).getTemplate();

        double quota = (double) templateSDK.getRule().getDiscount().getQuota();

        // 获取商品原始总价
        double goodsOriginalCost = settlementInfo.getCost();

        // 计算使用优惠券之后的总价
        double realCost = retain2Decimals(goodsOriginalCost * quota * 1.0 / 100);
        settlementInfo.setCost(
                realCost > minCost() ? realCost : minCost()
        );

        return settlementInfo;
    }
}
